package server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;


import com.google.gwt.user.server.rpc.RPCServletUtils;
//http://blog.csdn.net/leoyunfei/article/details/3611565

public class SaveDTOServiceImpl extends HttpServlet {
	   @Override
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	            throws ServletException, IOException {
	        String s = req.getParameter("txt");
	        JSONArray ja=null;
	        SaveDAO sd = new SaveDAO();
	        try {
				ja = sd.getDTO(s);
			} catch (SQLException e) {
				e.printStackTrace();
			}
	        resp.setCharacterEncoding("utf-8");
	        resp.getWriter().write(ja==null?"":ja.toString());
	    }
	    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	            throws ServletException, IOException {
	        String  str = RPCServletUtils.readContent(req, null,null);
			JSONObject p = (JSONObject) JSONSerializer.toJSON(str);
//			BasicDTO wd = (BasicDTO) JSONObject.toBean(p,BasicDTO.class);
	        SaveDAO sd= new SaveDAO();
	        try {
				sd.insertDTO(p);
			} catch (SQLException e) {
				e.printStackTrace();
			}
	        resp.setCharacterEncoding("utf-8");
	        resp.getWriter().write("server message!!!!" + str);
}
}
